#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

ll m, a, c, x, n, g;
void matmul(ll a[2][2], ll b[2][2], ll c[2][2]) {
  ll res[2][2] = {0};
  rep(i, 0, 1) rep(j, 0, 1) rep(k, 0, 1) {
    res[i][j] = (res[i][j] + (__int128_t)a[i][k] * b[k][j] % m) % m;
  }
  rep(i, 0, 1) rep(j, 0, 1) c[i][j] = res[i][j];
}
void matpow(ll b[2][2], ll p, ll c[2][2]) {
  ll res[2][2] = {{1, 0}, {0, 1}};
  while (p) {
    if (p & 1) matmul(res, b, res);
    matmul(b, b, b);
    p >>= 1;
  }
  rep(i, 0, 1) rep(j, 0, 1) c[i][j] = res[i][j];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> m >> a >> c >> x >> n >> g;
  a %= m, c %= m, x %= m;
  ll b[2][2] = {{a, 1}, {0, 1}};
  matpow(b, n, b);
  x = ((__int128_t)b[0][0] * x % m + (__int128_t)b[0][1] * c % m) % m % g;
  cout << x;
  return 0;
}